from py2neo import Graph, Node, Relationship, NodeMatcher
from utils import (
  log_message
)
from settings import (
  NEO4J_URL,
  NEO4J_USERNAME,
  NEO4J_PASSWORD
)
from consts import (
  NODE_MOVIE,
)
# 连接函数
def connect():
  try:
    graph = Graph(NEO4J_URL, auth=(NEO4J_USERNAME, NEO4J_PASSWORD), name='neo4j')
    log_message('数据库连接成功')
    return graph
  except:
    log_message('数据库连接失败')
    return None

def get_node_and_related_nodes(node_type, node_id):
  graph = connect()
  if not graph:
    return None

  query = """
    MATCH (m:Movie {id: $node_id})
    WITH m
    OPTIONAL MATCH p=(m)-[r]-(n)
    WITH m, COLLECT({Node: n, Relationship: r}) AS relationship
    RETURN {
      Movie: m,
      Relationship: relationship
    } AS Result
  """
  result = graph.run(query, node_id=node_id)


  log_message(result)

get_node_and_related_nodes(NODE_MOVIE, 862)